home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / misc / volume6 / doarch.pl < prev    next >
Encoding:
Text File  |  1989-04-23  |  6.6 KB  |  289 lines

  1. Newsgroups: comp.sources.misc
  2. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  3. Subject: v06i092: Archiving news articles
  4. Organization: Multihouse NV, the Netherlands
  5. Reply-To: jv@mh.nl (Johan Vromans)
  6.  
  7. Posting-number: Volume 6, Issue 92
  8. Submitted-by: jv@mh.nl (Johan Vromans)
  9. Archive-name: doarch.pl
  10.  
  11. [Ah, but can it handle multi-level by-name's?  (A few of the larger .misc
  12. submissions have been split into multiple subdirectories; jetroff, for
  13. example.  ++bsa]
  14.  
  15. The following perl script has proven to be handy when archiving and
  16. ordening lots of comp.sources articles.
  17.  
  18. It creates two links to each article, ordered bij volume, one link by
  19. name and the other link by issue. As an example, an article
  20. containing
  21.  
  22.     Archive-name: gl_plot/part04
  23.     Posting-number: Volume 18, Issue 24
  24.  
  25. will end up having links named
  26.  
  27.     ":Volume18/:By-Issue/v18i024" and
  28.     ":Volume18/:By-Name/gl_plot/part04" .
  29.  
  30. Of course, the script can easily be modified to do other things ...
  31.  
  32. The script is documented. An example how to use it is included.
  33.  
  34. --
  35. Johan Vromans             jv@mh.nl via european backbone (mcvax)
  36. Multihouse Automatisering bv        uucp: ..!{mcvax,hp4nl}!mh.nl!jv
  37. Doesburgweg 7                      phone: +31 1820 62944
  38. 2803 PL Gouda - The Netherlands                fax: +31 1820 62500
  39.  
  40. #!/bin/sh
  41. # This is a shell archive.  Remove anything before this line,
  42. # then unpack it by saving it in a file and typing "sh file".
  43. #
  44. # Wrapped by Johan Vromans <jv@mhres> on Wed Apr 19 18:04:52 1989
  45. #
  46. # This archive contains:
  47. #    doarch.pl    arch_all    
  48. #
  49.  
  50. LANG=""; export LANG
  51.  
  52. echo x - doarch.pl
  53. cat >doarch.pl <<'@EOF'
  54. #!/usr/bin/perl
  55.  
  56. # @(#)@ doarch    1.1
  57. #
  58. # Netnews source archiving
  59. #
  60. # This PERL program takes one input arg, the name of a Netnews
  61. # article, scans it for archiving headers, and links the article to
  62. # the appropriate places.
  63. #
  64. # Input syntax:
  65. #
  66. #    <standard headers or garbage>
  67. #    <empty line>
  68. #    <archiving headers or garbage>
  69. #    <empty line>
  70. #    <more garbage>
  71. #
  72. # The archiving headers recognized are:
  73. #
  74. #    Archive-name: NN
  75. #    Posting-number: Volume VV, Issue II
  76. #    Posting-number: Volume VV, Info AA
  77. #    Posting-number: Volume VV, Administrivia AA
  78. #    <garbage>: Volume VV, Issue II
  79. #    etc ...
  80. #
  81. # Upon completion, NN and VV must be known, as well as one of II or
  82. # AA. Multi-level directories are allowed in NN .
  83. #
  84. # The input article is linked to
  85. #
  86. #    ./:VolumeVV/:By-Issue/vVVINFAA
  87. # or
  88. #    ./:VolumeVV/:By-Name/NN
  89. #    ./:VolumeVV/:By-Issue/vVViIII
  90. #
  91. # One level of repost is handled.
  92.  
  93. $This_Article = $ARGV[0];
  94. $Archive_name = "";
  95. $Volume = 0;
  96. $Issue = 0;
  97. $Admin = 0;
  98.  
  99. $in_hdr = 2;        # looking for second section
  100.  
  101. while ( <> ) {
  102.  
  103.   # check which section if empty line, break out if we have had
  104.   # two sections.
  105.   $in_hdr-- if /^\n$/;
  106.   last if $in_hdr == 0;
  107.  
  108.   # look for archiving headers ...
  109.  
  110.   # Archive-name: funky-stuff/part02
  111.   if ( /^Archive-name:\s*(\S+)/i ) {
  112.     $Archive_name = $1;
  113. #    printf "=> Archive-name: \"%s\"\n", $Archive_name;
  114.     next;
  115.   }
  116.  
  117.   # Posting-number: Volume 18, Issue 24
  118.   if ( /^Posting-number:\s+Volume\s+(\d+),\s*Issue\s+(\d+)/i ) {
  119.     $Volume = $1;
  120.     $Issue = $2;
  121. #    printf "=> Posting-number: Volume %d, Issue %d\n", $Volume, $Issue;
  122.     next;
  123.   }
  124.  
  125.   # Posting-number: Volume 18, Info 4
  126.   if ( /^Posting-number:\s+Volume\s+(\d+),\s*Info\s+(\d+)/i ) {
  127.     $Volume = $1;
  128.     $Admin = $2;
  129. #    printf "=> Posting-number: Volume %d, Info %d\n", $Volume, $Admin;
  130.     next;
  131.   }
  132.  
  133.   # Posting-number: Volume 18, Administrivia 4
  134.   if ( /^Posting-number:\s+Volume\s+(\d+),\s*Administrivia\s+(\d+)/i ) {
  135.     $Volume = $1;
  136.     $Admin = $2;
  137. #    printf "=> Posting-number: Volume %d, Administrivia %d\n", $Volume, $Admin;
  138.     next;
  139.   }
  140.  
  141.   if ($Volume == 0 || ($Issue == 0 && $Admin == 0)) {
  142.  
  143.     if ( /Volume\s+(\d+),\s*Issue\s+(\d+)/i ) {
  144.       $Volume = $1;
  145.       $Issue = $2;
  146.       next;
  147.     }
  148.   
  149.     # Posting-number: Volume 18, Info 4
  150.     if ( /Volume\s+(\d+),\s*Info\s+(\d+)/i ) {
  151.       $Volume = $1;
  152.       $Admin = $2;
  153.       next;
  154.     }
  155.   
  156.     if ( /Volume\s+(\d+),\s*Administrivia\s+(\d+)/i ) {
  157.       $Volume = $1;
  158.       $Admin = $2;
  159.       next;
  160.     }
  161.   }
  162. }
  163.  
  164. # check for completeness
  165. if ( ($Archive_name eq "") || ($Volume == 0) ||
  166.     (($Issue == 0) && ($Admin == 0)) ) {
  167.   printf stderr "*> Error: incomplete entry %s: \"%s\" %d %d %d\n",
  168.     $This_Article, $Archive_name, $Volume, $Issue, $Admin;
  169. } else {
  170.  
  171. # ok
  172.   if ($Admin == 0) {    # not Administrivia
  173.  
  174.     do do_link($This_Article,
  175.            sprintf(":Volume%02d/:By-Name/%s",
  176.                $Volume, $Archive_name));
  177.     do do_link($This_Article,
  178.            sprintf(":Volume%02d/:By-Issue/v%02di%03d",
  179.                $Volume, $Volume, $Issue));
  180.   } else {
  181.     do do_link($This_Article,
  182.            sprintf(":Volume%02d/:By-Issue/v%02dINF%03d",
  183.                $Volume, $Volume, $Admin));
  184.   }
  185. }
  186.  
  187. sub do_link {
  188.   local($source) = shift(@_);
  189.   local($dest) = shift(@_);
  190.  
  191.   return 1
  192.     if do make_path(".", $dest) == 1;
  193.  
  194.   if (link($source,$dest) == 0) {
  195.     if ($! == 17) {
  196.       $dest .= "-REPOST";
  197.       if (link($source,$dest) == 0) {
  198.     do syserr("link(\"" . $source . "\",\"" . $dest . "\")");
  199.     return 1;
  200.       }
  201.     } else {
  202.       do syserr("link(\"" . $source . "\",\"" . $dest . "\")");
  203.       return 1;
  204.     }
  205.   }
  206.  
  207.   printf "linked " . $source . " => " . $dest . "\n";
  208. }
  209.  
  210. sub make_path {
  211.   local ($here) = shift(@_);
  212.   local ($dest) = shift(@_);
  213.  
  214. #  print "=> make_path: [" . $here . "/]" . $dest . "\n";
  215.   if ( $dest =~ m'^([^/]+)/(.+)' ) {
  216.     $first = $here . "/" . $1;
  217.     $next = $2;
  218.   } else {
  219.     return 0;
  220.   }
  221.  
  222.   if ( -d $first ) { 
  223.     return do make_path ($first, $next);
  224.   }
  225.  
  226.   if ( -e $first ) {
  227.     printf stderr "=> make_path: %s exists but not directory\n", $first;
  228.     return 1;
  229.   }
  230.  
  231. #  printf "=> mkdir %s\n", $first;
  232.   do syserr("mkdir(" . $first . ")") 
  233.     if system "mkdir " . $first;
  234.  
  235.   if ( -d $first ) { 
  236.     return do make_path ($first, $next);
  237.   }
  238.     
  239.   printf stderr "=> make_path: could not make path %s\n", $dest;
  240.   return 1;
  241. }
  242.  
  243. sub syserr {
  244.     printf stderr "%s: %s\n", shift(@_), $!;
  245. }
  246. @EOF
  247.  
  248. chmod 444 doarch.pl
  249.  
  250. echo x - arch_all
  251. cat >arch_all <<'@EOF'
  252. #!/bin/sh
  253.  
  254. loc=/usr/spool/oldnews/comp/sources
  255.  
  256. for this in unix misc games x
  257. do
  258.     cd $loc/:$this || exit 1
  259.     rm -fr *
  260.     for arch in news oldnews
  261.     do
  262.         for i in /usr/spool/$arch/comp/sources/$this/*
  263.         do
  264.             if [ -f $i ]
  265.             then
  266.                 $HOME/arch/doarch $i
  267.             fi
  268.         done
  269.     done
  270. done
  271. @EOF
  272.  
  273. chmod 755 arch_all
  274.  
  275. exit 0
  276. --
  277. Johan Vromans             jv@mh.nl via european backbone (mcvax)
  278. Multihouse Automatisering bv        uucp: ..!{mcvax,hp4nl}!mh.nl!jv
  279. Doesburgweg 7                      phone: +31 1820 62944
  280. 2803 PL Gouda - The Netherlands                fax: +31 1820 62500
  281.  
  282.  
  283. -- 
  284. Johan Vromans             jv@mh.nl via european backbone (mcvax)
  285. Multihouse Automatisering bv        uucp: ..!{mcvax,hp4nl}!mh.nl!jv
  286. Doesburgweg 7                      phone: +31 1820 62944
  287. 2803 PL Gouda - The Netherlands                fax: +31 1820 62500
  288.  
  289.